package com.zheng.wechat.mp.admin.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.JsonUtil;
import com.zheng.wechat.mp.common.constant.WechatMpResult;
import com.zheng.wechat.mp.common.constant.WechatMpResultConstant;
import com.zheng.wechat.mp.dao.model.WechatMpCategory;
import com.zheng.wechat.mp.dao.model.WechatMpCategoryExample;
import com.zheng.wechat.mp.rpc.api.WechatMpCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/8.
 */
@Controller
@RequestMapping("/manage/category")
@Api(value = "目录控制器", description = "目录控制器")
public class CategoryController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryController.class);

    @Autowired
    private WechatMpCategoryService wechatMpCategoryService;
    /**
     * 目录首页
     * @return
     */
    @ApiOperation(value = "目录首页")
    @RequiresPermissions("wechatmp:category:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/category/index.jsp";
    }

    @ApiOperation(value = "目录树")
    @RequiresPermissions("wechatmp:category:read")
    @RequestMapping(value = "/tree", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object tree(
            @RequestParam(required = false) Integer id) {
        WechatMpCategoryExample wechatMpCategoryExample = new WechatMpCategoryExample();
        WechatMpCategoryExample.Criteria criteria = wechatMpCategoryExample.createCriteria();
        criteria.andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        if (id!=null) {
            criteria.andPidEqualTo(id);
        }else{
            criteria.andPidIsNull();
        }
        List<WechatMpCategory> rows = wechatMpCategoryService.selectByExample(wechatMpCategoryExample);
        List<HashMap<String, Object>> resultList = new ArrayList<HashMap<String, Object>>();
        for (WechatMpCategory category : rows) {
            HashMap<String, Object> tempMap = new HashMap<String, Object>();
            tempMap.put("id", category.getId());
            tempMap.put("name", category.getName());
            if (category.getPid()!=null) {
                tempMap.put("pId", category.getPid());
            }
//			tempMap.put("checked", agent.getIsAgent() ? "true":"false");
            //			tempMap.put("nochecked", agent.getIsAgent() ? "true":"false");
            tempMap.put("isParent", "true");
            tempMap.put("iconSkin", "");
            resultList.add(tempMap);
        }
        return resultList;
    }

    @ApiOperation(value = "目录列表")
    @RequiresPermissions("wechatmp:category:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        WechatMpCategoryExample wechatMpCategoryExample = new WechatMpCategoryExample();
        wechatMpCategoryExample.createCriteria().andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            wechatMpCategoryExample.setOrderByClause(sort + " " + order);
        }else{
            wechatMpCategoryExample.setOrderByClause("ctime asc");
        }
        if (StringUtils.isNotBlank(search)) {
            wechatMpCategoryExample.or()
                    .andNameLike("%" + search + "%");
        }
        List<WechatMpCategory> rows = wechatMpCategoryService.selectByExample(wechatMpCategoryExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        return result;
    }

    @ApiOperation(value = "新增目录")
    @RequiresPermissions("wechatmp:category:create")
    @RequestMapping(value = "/create/{id}", method = {RequestMethod.GET})
    public String create(@PathVariable("id") int id, ModelMap modelMap) {
        if(StringUtils.isNotBlank(id+"")){
            WechatMpCategory wechatMpCategory  = wechatMpCategoryService.selectByPrimaryKey(id);
            modelMap.put("pcategory", wechatMpCategory);
        }
        WechatMpCategoryExample example = new WechatMpCategoryExample();
        example.createCriteria().andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        List<WechatMpCategory> sechatMpCategorys = wechatMpCategoryService.selectByExample(example);
        modelMap.put("sechatMpCategorys", JsonUtil.object2Json(sechatMpCategorys));
        return "/manage/category/create.jsp";
    }

    @ApiOperation(value = "新增目录")
    @RequiresPermissions("wechatmp:category:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(WechatMpCategory wechatMpCategory ) {
        long time = System.currentTimeMillis();
        wechatMpCategory.setCtime(time);
        wechatMpCategory.setCuser(getLoginUserName());
        if(wechatMpCategory.getOrders() == null){
            wechatMpCategory.setOrders(time);
        }
        int count = wechatMpCategoryService.insertSelective(wechatMpCategory);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除目录")
    @RequiresPermissions("wechatmp:category:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = wechatMpCategoryService.deleteByPrimaryKeys(ids);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改目录")
    @RequiresPermissions("wechatmp:category:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        WechatMpCategory wechatMpCategory  = wechatMpCategoryService.selectByPrimaryKey(id);
        modelMap.put("category", wechatMpCategory);
        if(wechatMpCategory.getPid()!=null){
            WechatMpCategory parentWechatMpCategory = wechatMpCategoryService.selectByPrimaryKey(wechatMpCategory.getPid());
            modelMap.put("pcategory", parentWechatMpCategory);
        }
        WechatMpCategoryExample example = new WechatMpCategoryExample();
        example.createCriteria().andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        List<WechatMpCategory> wechatMpCategorys = wechatMpCategoryService.selectByExample(example);
        modelMap.put("wechatMpCategorys", JsonUtil.object2Json(wechatMpCategorys));
        return "/manage/category/update.jsp";
    }

    @ApiOperation(value = "修改目录")
    @RequiresPermissions("wechatmp:category:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") int id, WechatMpCategory wechatMpCategory ) {
        wechatMpCategory.setId(id);
        int count = wechatMpCategoryService.updateByPrimaryKeySelective(wechatMpCategory);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }


}
